<?php
/**
 * 作用：微信管理
 * 作者: shijy@soe-soe.com
 * 日期: 2016/4/27 13:41
 * 公司: 浙江宣逸网络科技有限公司<www.soe-soe.com>
 */
namespace Admin\Controller;
use Admin\BaseController;
use Common\Model\WeixinModel;

class WeixinController extends BaseController{
	private $dataSuccess = array('status'=>0,'msg'=>'成功');
	private $dataFailure = array('status'=>1,'msg'=>'无效参数');

	public function index(){
		$list=WeixinModel::getRuleList(0,50);
		$this->smarty->assign('list',$list['data']);
		$this->smarty->display('Weixin/keyWord.html');
	}

	public function kefu(){

		

		$this->smarty->display('Weixin/kefu.html');
	}

	/**
	 * 删除规则
	 * Author: shijy
	 */
	public function deleteRule(){
		$id=I('post.id');
		$this->ajaxReturn(WeixinModel::deleteRule($id));

	}

	/**
	 * 回复规则修改添加
	 * Author: shijy
	 */
	public function updateRule(){

		//关键词设定
		$id=I('post.id',0,'int');
		$replyType=I('post.replyType','');
		$title=I('post.title','');
		$keyWord=I('post.keyWord','','trim');
		$beginTime=I('post.beginTime','');
		$endTime=I('post.endTime','');
		$beginTime	= strtotime($beginTime);
		$endTime	= strtotime($endTime);

		if(strlen($title)<1){
			$this->dataFailure['msg']='规则标题必须填写';
			$this->ajaxReturn($this->dataFailure);
		}

		if(strlen($keyWord)<1){
			$this->dataFailure['msg']='关键词必须填写';
			$this->ajaxReturn($this->dataFailure);
		}

		//以下为回复设定
		$replyContent=I('post.replyContent','');
		$replyTitle=I('post.replyTitle','');
		$URL=I('post.URL','');
		$quota=I('post.quota',0,'int');
		$stock=I('post.stock',0,'int');
		$replyImg=I('post.replyImg','');

		$data=array(
			'title'=>$title,
			'module'=>$replyType,
			'content'=>$keyWord,
			'type'=>WeixinModel::MATCH_TYPE_STRICT,
			'beginTime'=>$beginTime,
			'endTime'=>$endTime,
			'status'=>1
		);

		$ruleTable	= 'ims_rule_keyword';
		$replyTable		= 'ims_reply_'.$replyType;

		switch($replyType){
			case WeixinModel::REPLY_TYPE_BASIC:
				if(strlen($replyContent)<1){
					$this->dataFailure['msg']='回复内容不能为空';
					$this->ajaxReturn( $this->dataFailure);
				}
				$ruleID=M($ruleTable,null,DB_MAIN_CFG)->add($data);
				if($ruleID===false){
					$this->dataFailure['msg']='规则添加失败';
					$this->ajaxReturn( $this->dataFailure);
				}

				M($replyTable,null,DB_MAIN_CFG)->add(array(
					'rid'=>$ruleID,
					'content'=>$replyContent
				));

				break;
			case WeixinModel::REPLY_TYPE_NEWS:

				if(strlen($replyTitle)<1){
					$this->dataFailure['msg']='回复标题不能为空';
					$this->ajaxReturn( $this->dataFailure);
				}
				if(strlen($URL)<1){
					$this->dataFailure['msg']='跳转地址不能为空';
					$this->ajaxReturn( $this->dataFailure);
				}
				if(strlen($replyImg)<1){
					$this->dataFailure['msg']='封面图片必须指定';
					$this->ajaxReturn( $this->dataFailure);
				}

				$ruleID=M($ruleTable,null,DB_MAIN_CFG)->add($data);
				if($ruleID===false){
					$this->dataFailure['msg']='规则添加失败';
					$this->ajaxReturn( $this->dataFailure);
				}
				M($replyTable,null,DB_MAIN_CFG)->add(array(
					'rid'=>$ruleID,
					'title'=>$replyTitle,
					'thumb'=>$replyImg,
					'url'=>$URL,
					'createtime'=>time(),
					'parent_id'=>0
				));
				break;
			case WeixinModel::REPLY_TYPE_CARD:
			case WeixinModel::REPLY_TYPE_VOICE:
			case WeixinModel::REPLY_TYPE_IMAGE:
			case WeixinModel::REPLY_TYPE_MUSIC:
			case WeixinModel::REPLY_TYPE_USERAPI:
			case WeixinModel::REPLY_TYPE_VIDEO:
			default:
				break;
		}
		$this->ajaxReturn($this->dataSuccess);


	}


}
